package com.ttpai.boot.lbs.controller;

import com.ttpai.boot.lbs.model.RoutePlanReportVO;
import com.ttpai.boot.lbs.model.assist.GroupByVO;
import com.ttpai.boot.lbs.service.*;
import com.ttpai.boot.lbs.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by kail on 2017/2/25.
 */
@Controller
@RequestMapping("/route_plan_report")
public class RoutePlanReportController {

    @Autowired
    private RoutePlanReportService routePlanReportService;
    @Autowired
    private DynamicRoutePlanService dynamicRoutePlanService;
    @Autowired
    private DynamicRoutePlanService0Dis dynamicRoutePlanService0Dis;
    @Autowired
    private DynamicRoutePlanService2 dynamicRoutePlanService2;
    @Autowired
    private MetroPointPlanRouteService metroPointPlanRouteService;

    @Autowired
    private BaiduLbsService baiduLbsService;

    @RequestMapping("/all_ll_date")
    public String selectAllLLDate(Model mv) {
        List<GroupByVO> allLlDate = routePlanReportService.selectHaveLLDayGroupByAll();
        mv.addAttribute("allLlDate", allLlDate);
        return "route_plan_report/all_ll_date";
    }

    @RequestMapping("/all_ll_date/detail")
    public String selectAllLLDateDetail(@RequestParam(value = "date", required = false) String dateStr, Model mv) {
        if (StringUtils.hasText(dateStr)) {
            List<RoutePlanReportVO> reports = routePlanReportService.selectRoutePlanReportByDate(DateUtil.format(dateStr));
            mv.addAttribute("reports", reports);
        }
        return "route_plan_report/all_ll_date__detail";
    }

    @RequestMapping("/all_ll_date/detail_baidu")
    public String selectAllLLDateDetailBaidu(
            @RequestParam(value = "city", required = false) String city,
            @RequestParam(value = "date", required = false) String dateStr,
            @RequestParam(value = "type", required = false) Integer type,
            @RequestParam(value = "hideOtherLine", required = false) Integer hideOtherLine,
            @RequestParam(value = "lines", required = false) List<String> lines,
            Model mv) {

        if (StringUtils.hasText(dateStr)) {
            Date date = DateUtil.format(dateStr);
            if (null == type || type.equals(0)) {
                mv.addAttribute("reports", routePlanReportService.selectRoutePlanReportByDate(date, city));
            } else {
                Map<String, List<RoutePlanReportVO>> reports = null;
                if (type.equals(1)) {
                    // 人工路线
                    reports = routePlanReportService.selectCurrentRoutePlan(date, city);
                } else if (type.equals(2)) {
                    // 第一版程序路线
                    reports = dynamicRoutePlanService.buildDynamicPlain(date, city);
                } else if (type.equals(3)) {
                    // 无路程时间
                    reports = dynamicRoutePlanService0Dis.buildDynamicPlain(date, city);
                } else if (type.equals(4)) {
                    // 按照时间范围
                    reports = dynamicRoutePlanService2.buildDynamicPlain(date, city);
                } else if (type.equals(5)) {
                    // 地铁规划
                    reports = metroPointPlanRouteService.buildMetroDynamicPlain(date, city);
                }
                mv.addAttribute("reports", reports);
                if (null != lines && !lines.isEmpty()) {
                    mv.addAttribute("dandianReports", routePlanReportService.showCheckedLine(lines, reports, hideOtherLine));
                }
            }
        }


        mv.addAttribute("dates", routePlanReportService.selectHaveLLDayNotZero());
        return "route_plan_report/all_ll_date__detail_baidu";
    }

    /**
     * 抓取某一天的经纬度
     */
    @ResponseBody
    @RequestMapping("/all_ll_date/fetch")
    public String selectAllLLDateDetail(@RequestParam("date") String dateStr) throws IOException {
        baiduLbsService.fetchLngLat(DateUtil.format(dateStr));
        return "获取完成，请关闭当前页面";
    }

    @RequestMapping("/readme")
    public String readme() {
        return "route_plan_report/readme";
    }

}
